package com.quanyan.club.web;

import com.alibaba.fastjson.JSON;
import com.quanyan.api.APIResponse;
import com.quanyan.api.enums.ApiResponseEnum;
import com.quanyan.club.entity.vo.req.movement.ReqClubListInfo;
import com.quanyan.club.entity.vo.req.movement.ReqPointPrise;
import com.quanyan.club.entity.vo.req.movement.ReqSendCommentMovement;
import com.quanyan.club.entity.vo.req.movement.ReqSubmitMovement;
import com.quanyan.club.entity.vo.resp.movement.*;
import com.quanyan.club.service.ClubMovementService;
import com.quanyan.common.Plugin.PageObj;
import com.quanyan.common.exception.BizException;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author xian-guo
 * @email 498152332@qq.com
 * @date 2016/12/1.
 */
@RestController
@RequestMapping(value = "/api/club/movement/")
public class ClubMovementController {
    private static Logger logger = Logger.getLogger(ClubActivityController.class);
    @Autowired
    private ClubMovementService clubMovementService;

    /**
     * 俱乐部关联的未取消活动
     * @param request
     * @param reqClubListInfo
     * @return
     */
    @RequestMapping(value = "getClubBindActivity", method = RequestMethod.POST)
    public APIResponse<PageObj<List<RespClubActivityInfo>>> getClubBindActivity(HttpServletRequest request,@RequestBody ReqClubListInfo reqClubListInfo){
        logger.warn("getClubBindActivity--------------->"+JSON.toJSONString(reqClubListInfo));
        getUid(request,true);
        return clubMovementService.getClubBindActivity(reqClubListInfo);
    }

    /**
     * 发布动态
     * @param request
     * @param reqSubmitMovement
     * @return
     */
    @RequestMapping(value = "submitMovementInfo", method = RequestMethod.POST)
    public APIResponse submitMovementInfo(HttpServletRequest request,@RequestBody ReqSubmitMovement reqSubmitMovement){
        logger.warn("submitMovementInfo--------------->"+JSON.toJSONString(reqSubmitMovement));
        Integer uid=getUid(request,true);
        reqSubmitMovement.setuId(uid);
        return clubMovementService.submitMovementInfo(reqSubmitMovement);
    }


    /**
     * 获取俱乐部动态列表
     * 不需要登录
     * @param reqClubListInfo
     * @return
     */
    @RequestMapping(value = "getMovementListInfo", method = RequestMethod.POST)
    public APIResponse<PageObj<List<RespMovementListInfo>>> getMovementListInfo(HttpServletRequest request,@RequestBody ReqClubListInfo reqClubListInfo){
        logger.warn("getMovementListInfo--------------->"+JSON.toJSONString(reqClubListInfo));
        Integer uid=getUid(request,false);
        if (uid>0){
            Map<String,Object> paramMap=reqClubListInfo.getParamMap();
            paramMap.put("uId",uid);
        }
        return clubMovementService.getMovementListInfo(reqClubListInfo);
    }

    /**
     * 点赞
     * @param request
     * @param reqPointPrise
     * @return
     */
    @RequestMapping(value = "setPointPrise", method = RequestMethod.POST)
    public APIResponse setPointPrise(HttpServletRequest request, @RequestBody ReqPointPrise reqPointPrise){
        logger.warn("setPointPrise--------------->"+JSON.toJSONString(reqPointPrise));
        int uid=getUid(request,true);
        reqPointPrise.setuId(uid);
        byte type=reqPointPrise.getType();
        if (type !=1 && type !=2) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        return clubMovementService.setPointPrise(reqPointPrise);
    }

    /**
     * 发表评论
     * @param request
     * @param reqSendCommentMovement
     * @return
     */
    @RequestMapping(value = "sendCommentToMovement", method = RequestMethod.POST)
    public APIResponse sendCommentToMovement(HttpServletRequest request, @RequestBody ReqSendCommentMovement reqSendCommentMovement){
        logger.warn("sendCommentToMovement--------------->"+JSON.toJSONString(reqSendCommentMovement));
        int uid=getUid(request,true);
        reqSendCommentMovement.setFromUserId(uid);
        return clubMovementService.sendCommentToMovement(reqSendCommentMovement);
    }

    /**
     * 动态详情
     * @param request
     * @param paramMap
     * @return
     */
    @RequestMapping(value = "getMovementDetail", method = RequestMethod.POST)
    public APIResponse<RespClubMovementDetail> getMovementDetail(HttpServletRequest request, @RequestBody Map<String,Object> paramMap){
        logger.warn("getMovementDetail--------------->"+JSON.toJSONString(paramMap));
        int uid=getUid(request,false);
        return clubMovementService.getMovementDetail((Integer) paramMap.get("mvId"),uid);
    }

    /**
     * 动态评论
     * @param request
     * @param reqClubListInfo
     * @return
     */
    @RequestMapping(value = "getMovementComment", method = RequestMethod.POST)
    public APIResponse<PageObj<List<MovementComment>>> getMovementComment(HttpServletRequest request, @RequestBody ReqClubListInfo reqClubListInfo){
        logger.warn("getMovementComment--------------->"+JSON.toJSONString(reqClubListInfo));
        return clubMovementService.getMovementComment(reqClubListInfo);
    }

    /**
     * 点赞人列表
     * @param request
     * @param reqClubListInfo
     * @return
     */
    @RequestMapping(value = "getMovementPraiseList", method = RequestMethod.POST)
    public APIResponse<PageObj<List<PraiseListUserInfo>>> getMovementPraiseList(HttpServletRequest request, @RequestBody ReqClubListInfo reqClubListInfo){
        logger.warn("getMovementPraiseList--------------->"+JSON.toJSONString(reqClubListInfo));
        int uId=getUid(request,false);
        if (uId!=-1){
            Map<String,Object> paramMap=reqClubListInfo.getParamMap();
            paramMap.put("uId",uId);
            reqClubListInfo.setParamMap(paramMap);
        }
        return clubMovementService.getMovementPraiseList(reqClubListInfo);
    }

    /**
     * 删除评论 and  动态
     * clubId
     * type 1:动态 2：评论
     * paramId  动态id or 评论id
     * @param request
     * @param paramMap
     * @return
     */
    @RequestMapping(value = "delMovementCommentAndMovement", method = RequestMethod.POST)
    public APIResponse<PageObj<List<PraiseListUserInfo>>> delMovementCommentAndMovement(HttpServletRequest request, @RequestBody Map<String,Object> paramMap){
        logger.warn("delMovementCommentAndMovement--------------->"+JSON.toJSONString(paramMap));
        int uId=getUid(request,true);
        paramMap.put("uId",uId);
        if (!paramMap.containsKey("type")){
            return APIResponse.returnFail("删除类型不能为空");
        }
        if (!paramMap.containsKey("paramId") && !paramMap.containsKey("clubId")){
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_CANT_BE_EMPTY);
        }
        return clubMovementService.delMovementCommentAndMovement(paramMap);
    }


































    public int getUid(HttpServletRequest request,boolean isNeedLogin){
        Object uidObj = request.getAttribute("uid");
        int uid = -1;
        if (uidObj != null){
            uid = Integer.valueOf(String.valueOf(uidObj));
        }
        if (uid == -1 && isNeedLogin){
            throw new BizException(ApiResponseEnum.NEED_USER_LOGIN);
        }
        return uid;
    }
}
